package com.huawei.secure.android.common.webview;

import android.annotation.TargetApi;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.ClientCertRequest;
import android.webkit.HttpAuthHandler;
import android.webkit.RenderProcessGoneDetail;
import android.webkit.SafeBrowsingResponse;
import android.webkit.SslErrorHandler;
import android.webkit.URLUtil;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebResourceResponse;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import com.huawei.secure.android.common.util.LogsUtil;
import com.huawei.secure.android.common.webview.WebViewLoadCallBack.ErrorCode;
import java.util.Arrays;
import java.util.Map;

public class SafeWebView extends WebView {
    private static final String TAG = "SafeWebView";
    private String B;
    private String[] C;
    private String[] D;
    private String[] E;
    private WebViewLoadCallBack F;

    static class a implements OnClickListener {
        private final SslErrorHandler G;

        a(SslErrorHandler sslErrorHandler) {
            this.G = sslErrorHandler;
        }

        public void onClick(DialogInterface dialogInterface, int i) {
            this.G.cancel();
        }
    }

    static class b implements OnClickListener {
        private final SslErrorHandler G;

        b(SslErrorHandler sslErrorHandler) {
            this.G = sslErrorHandler;
        }

        public void onClick(DialogInterface dialogInterface, int i) {
            this.G.proceed();
        }
    }

    final class c extends WebViewClient {
        private WebViewClient H;
        private boolean I;
        final /* synthetic */ SafeWebView J;

        private c(SafeWebView safeWebView, WebViewClient webViewClient, boolean z) {
            this.J = safeWebView;
            this.H = webViewClient;
            this.I = z;
        }

        @TargetApi(21)
        @Nullable
        public WebResourceResponse shouldInterceptRequest(WebView webView, WebResourceRequest webResourceRequest) {
            if (this.H != null) {
                return this.H.shouldInterceptRequest(webView, webResourceRequest);
            }
            return super.shouldInterceptRequest(webView, webResourceRequest);
        }

        @TargetApi(11)
        @Nullable
        public WebResourceResponse shouldInterceptRequest(WebView webView, String str) {
            if (this.H != null) {
                return this.H.shouldInterceptRequest(webView, str);
            }
            return super.shouldInterceptRequest(webView, str);
        }

        public boolean shouldOverrideUrlLoading(WebView webView, String str) {
            if (this.H != null) {
                return this.H.shouldOverrideUrlLoading(webView, str);
            }
            return super.shouldOverrideUrlLoading(webView, str);
        }

        @TargetApi(24)
        public boolean shouldOverrideUrlLoading(WebView webView, WebResourceRequest webResourceRequest) {
            if (this.H != null) {
                return this.H.shouldOverrideUrlLoading(webView, webResourceRequest);
            }
            return super.shouldOverrideUrlLoading(webView, webResourceRequest);
        }

        public void onPageStarted(WebView webView, String str, Bitmap bitmap) {
            if (this.H != null && !this.I) {
                this.H.onPageStarted(webView, str, bitmap);
            } else if (this.J.isWhiteListUrl(str)) {
                super.onPageStarted(webView, str, bitmap);
            } else {
                this.J.onCheckError(webView, str);
            }
        }

        public void onPageFinished(WebView webView, String str) {
            if (this.H != null) {
                this.H.onPageFinished(webView, str);
            } else {
                super.onPageFinished(webView, str);
            }
        }

        public void onLoadResource(WebView webView, String str) {
            if (this.H != null) {
                this.H.onLoadResource(webView, str);
            } else {
                super.onLoadResource(webView, str);
            }
        }

        @TargetApi(23)
        public void onPageCommitVisible(WebView webView, String str) {
            if (this.H != null) {
                this.H.onPageCommitVisible(webView, str);
            } else {
                super.onPageCommitVisible(webView, str);
            }
        }

        public void onTooManyRedirects(WebView webView, Message message, Message message2) {
            if (this.H != null) {
                this.H.onTooManyRedirects(webView, message, message2);
            } else {
                super.onTooManyRedirects(webView, message, message2);
            }
        }

        public void onReceivedError(WebView webView, int i, String str, String str2) {
            if (this.H != null) {
                this.H.onReceivedError(webView, i, str, str2);
            } else {
                super.onReceivedError(webView, i, str, str2);
            }
        }

        @TargetApi(23)
        public void onReceivedError(WebView webView, WebResourceRequest webResourceRequest, WebResourceError webResourceError) {
            if (this.H != null) {
                this.H.onReceivedError(webView, webResourceRequest, webResourceError);
            } else {
                super.onReceivedError(webView, webResourceRequest, webResourceError);
            }
        }

        @TargetApi(23)
        public void onReceivedHttpError(WebView webView, WebResourceRequest webResourceRequest, WebResourceResponse webResourceResponse) {
            if (this.H != null) {
                this.H.onReceivedHttpError(webView, webResourceRequest, webResourceResponse);
            } else {
                super.onReceivedHttpError(webView, webResourceRequest, webResourceResponse);
            }
        }

        public void onFormResubmission(WebView webView, Message message, Message message2) {
            if (this.H != null) {
                this.H.onFormResubmission(webView, message, message2);
            } else {
                super.onFormResubmission(webView, message, message2);
            }
        }

        public void doUpdateVisitedHistory(WebView webView, String str, boolean z) {
            if (this.H != null) {
                this.H.doUpdateVisitedHistory(webView, str, z);
            } else {
                super.doUpdateVisitedHistory(webView, str, z);
            }
        }

        @TargetApi(21)
        public void onReceivedClientCertRequest(WebView webView, ClientCertRequest clientCertRequest) {
            if (this.H != null) {
                this.H.onReceivedClientCertRequest(webView, clientCertRequest);
            } else {
                super.onReceivedClientCertRequest(webView, clientCertRequest);
            }
        }

        public void onReceivedHttpAuthRequest(WebView webView, HttpAuthHandler httpAuthHandler, String str, String str2) {
            if (this.H != null) {
                this.H.onReceivedHttpAuthRequest(webView, httpAuthHandler, str, str2);
            } else {
                super.onReceivedHttpAuthRequest(webView, httpAuthHandler, str, str2);
            }
        }

        public boolean shouldOverrideKeyEvent(WebView webView, KeyEvent keyEvent) {
            if (this.H != null) {
                return this.H.shouldOverrideKeyEvent(webView, keyEvent);
            }
            return super.shouldOverrideKeyEvent(webView, keyEvent);
        }

        public void onUnhandledKeyEvent(WebView webView, KeyEvent keyEvent) {
            if (this.H != null) {
                this.H.onUnhandledKeyEvent(webView, keyEvent);
            } else {
                super.onUnhandledKeyEvent(webView, keyEvent);
            }
        }

        @RequiresApi(api = 26)
        public boolean onRenderProcessGone(WebView webView, RenderProcessGoneDetail renderProcessGoneDetail) {
            if (this.H != null) {
                return this.H.onRenderProcessGone(webView, renderProcessGoneDetail);
            }
            return super.onRenderProcessGone(webView, renderProcessGoneDetail);
        }

        @RequiresApi(api = 27)
        public void onSafeBrowsingHit(WebView webView, WebResourceRequest webResourceRequest, int i, SafeBrowsingResponse safeBrowsingResponse) {
            if (this.H != null) {
                this.H.onSafeBrowsingHit(webView, webResourceRequest, i, safeBrowsingResponse);
            } else {
                super.onSafeBrowsingHit(webView, webResourceRequest, i, safeBrowsingResponse);
            }
        }

        public void onScaleChanged(WebView webView, float f, float f2) {
            if (this.H != null) {
                this.H.onScaleChanged(webView, f, f2);
            } else {
                super.onScaleChanged(webView, f, f2);
            }
        }

        @TargetApi(12)
        public void onReceivedLoginRequest(WebView webView, String str, @Nullable String str2, String str3) {
            if (this.H != null) {
                this.H.onReceivedLoginRequest(webView, str, str2, str3);
            } else {
                super.onReceivedLoginRequest(webView, str, str2, str3);
            }
        }

        @TargetApi(8)
        public void onReceivedSslError(WebView webView, SslErrorHandler sslErrorHandler, SslError sslError) {
            if (this.H != null) {
                this.H.onReceivedSslError(webView, sslErrorHandler, sslError);
            } else {
                super.onReceivedSslError(webView, sslErrorHandler, sslError);
            }
        }
    }

    public SafeWebView(Context context) {
        super(context);
        b();
    }

    public SafeWebView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        b();
    }

    public SafeWebView(Context context, AttributeSet attributeSet, int i) {
        super(context, attributeSet, i);
        b();
    }

    @TargetApi(21)
    public SafeWebView(Context context, AttributeSet attributeSet, int i, int i2) {
        super(context, attributeSet, i, i2);
        b();
    }

    @TargetApi(9)
    public String[] getWhitelist() {
        return this.C == null ? null : (String[]) Arrays.copyOf(this.C, this.C.length);
    }

    @TargetApi(9)
    public void setWhitelist(String[] strArr) {
        this.C = strArr == null ? null : (String[]) Arrays.copyOf(strArr, strArr.length);
    }

    @TargetApi(9)
    public String[] getWhitelistWithPath() {
        return this.E == null ? null : (String[]) Arrays.copyOf(this.E, this.E.length);
    }

    @TargetApi(9)
    public void setWhitelistWithPath(String[] strArr) {
        this.E = strArr == null ? null : (String[]) Arrays.copyOf(strArr, strArr.length);
    }

    public String[] getWhitelistNotMathcSubDomain() {
        return this.D == null ? null : (String[]) Arrays.copyOf(this.D, this.D.length);
    }

    public void setWhitelistNotMathcSubDomain(String[] strArr) {
        this.D = strArr == null ? null : (String[]) Arrays.copyOf(strArr, strArr.length);
    }

    public String getDefaultErrorPage() {
        return this.B;
    }

    public void setDefaultErrorPage(String str) {
        this.B = str;
    }

    public void loadUrl(String str) {
        if (isHttpUrl(str)) {
            Log.e(TAG, "loadUrl: http url , not safe");
            if (!TextUtils.isEmpty(this.B)) {
                super.loadUrl(this.B);
                return;
            } else if (getWebViewLoadCallBack() != null) {
                Log.e(TAG, "WebViewLoadCallBack");
                getWebViewLoadCallBack().onCheckError(str, ErrorCode.HTTP_URL);
                return;
            } else {
                return;
            }
        }
        super.loadUrl(str);
    }

    public void postUrl(String str, byte[] bArr) {
        if (isHttpUrl(str)) {
            Log.e(TAG, "postUrl: http url , not safe");
            if (!TextUtils.isEmpty(this.B)) {
                super.postUrl(this.B, bArr);
                return;
            } else if (getWebViewLoadCallBack() != null) {
                Log.e(TAG, "WebViewLoadCallBack");
                getWebViewLoadCallBack().onCheckError(str, ErrorCode.HTTP_URL);
                return;
            } else {
                return;
            }
        }
        super.postUrl(str, bArr);
    }

    public void loadUrl(String str, Map<String, String> map) {
        if (isHttpUrl(str)) {
            Log.e(TAG, "loadUrl: http url , not safe");
            if (!TextUtils.isEmpty(this.B)) {
                super.loadUrl(this.B, map);
                return;
            } else if (getWebViewLoadCallBack() != null) {
                Log.e(TAG, "WebViewLoadCallBack");
                getWebViewLoadCallBack().onCheckError(str, ErrorCode.HTTP_URL);
                return;
            } else {
                return;
            }
        }
        super.loadUrl(str, map);
    }

    public void loadDataWithBaseURL(String str, String str2, String str3, String str4, String str5) {
        if (isHttpUrl(str)) {
            Log.e(TAG, "loadDataWithBaseURL: http url , not safe");
            if (!TextUtils.isEmpty(this.B)) {
                super.loadDataWithBaseURL(this.B, str2, str3, str4, str5);
                return;
            } else if (getWebViewLoadCallBack() != null) {
                Log.e(TAG, "WebViewLoadCallBack");
                getWebViewLoadCallBack().onCheckError(str, ErrorCode.HTTP_URL);
                return;
            } else {
                return;
            }
        }
        super.loadDataWithBaseURL(str, str2, str3, str4, str5);
    }

    private boolean isHttpUrl(String str) {
        return URLUtil.isHttpUrl(str);
    }

    public void setWebViewClient(WebViewClient webViewClient) {
        super.setWebViewClient(new c(webViewClient, true));
    }

    public void setWebViewClient(WebViewClient webViewClient, boolean z) {
        super.setWebViewClient(new c(webViewClient, z));
    }

    private void b() {
        SafeWebSettings.initWebviewAndSettings(this);
        setWebViewClient(null);
    }

    @TargetApi(9)
    public boolean isWhiteListUrl(String str) {
        if (TextUtils.isEmpty(str)) {
            LogsUtil.e(TAG, "url is null");
            return false;
        } else if (!URLUtil.isNetworkUrl(str)) {
            return true;
        } else {
            String[] whitelistWithPath = getWhitelistWithPath();
            String[] whitelistNotMathcSubDomain = getWhitelistNotMathcSubDomain();
            String[] whitelist = getWhitelist();
            if (whitelistWithPath != null && whitelistWithPath.length != 0) {
                return UriUtil.isUrlHostAndPathInWhitelist(str, whitelistWithPath);
            }
            if (whitelistNotMathcSubDomain == null || whitelistNotMathcSubDomain.length == 0) {
                return UriUtil.isUrlHostInWhitelist(str, whitelist);
            }
            return UriUtil.isUrlHostSameWhitelist(str, whitelistNotMathcSubDomain);
        }
    }

    public final void onCheckError(WebView webView, String str) {
        LogsUtil.e(TAG, "onCheckError url is not in white list ", str);
        webView.stopLoading();
        Object defaultErrorPage = getDefaultErrorPage();
        if (!TextUtils.isEmpty(defaultErrorPage)) {
            webView.loadUrl(defaultErrorPage);
        } else if (getWebViewLoadCallBack() != null) {
            Log.e(TAG, "onPageStarted WebViewLoadCallBack");
            getWebViewLoadCallBack().onCheckError(str, ErrorCode.URL_NOT_IN_WHITE_LIST);
        }
    }

    protected final void showNoticeWhenSSLErrorOccurred(@Nullable String str, @NonNull String str2, @NonNull String str3, @NonNull String str4, @NonNull SslErrorHandler sslErrorHandler) {
        Builder builder = new Builder(getContext());
        if (!TextUtils.isEmpty(str)) {
            builder.setTitle(str);
        }
        OnClickListener bVar = new b(sslErrorHandler);
        OnClickListener aVar = new a(sslErrorHandler);
        builder.setMessage(str2);
        builder.setPositiveButton(str3, bVar);
        builder.setNegativeButton(str4, aVar);
        builder.create().show();
    }

    public WebViewLoadCallBack getWebViewLoadCallBack() {
        return this.F;
    }

    public void setWebViewLoadCallBack(WebViewLoadCallBack webViewLoadCallBack) {
        this.F = webViewLoadCallBack;
    }
}
